﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace AnimalWars2
{
    public class GameMenu : GameScreen
    {
        List<Button> m_ButonList = new List<Button>();
        Texture2D m_MenuBackgroundTexture;
        protected SoundEffect m_MenuSoundEffect;
        protected SoundEffectInstance m_MenuSoundEffectInstance;

        public GameMenu(Game GameRef)
            : base(GameRef)
        {
            m_ButonList.Add(new Button(GameRef, "Start Game", 225, 335, Constants.BUTTON_ASSET_NAME, new ButtonClickDelegate(startButtonClick)));
            m_ButonList.Add(new Button(GameRef, "About", 225, 383, Constants.BUTTON_ASSET_NAME, new ButtonClickDelegate(aboutButtonClick)));
            m_ButonList.Add(new Button(GameRef, "Quit", 225, 430, Constants.BUTTON_ASSET_NAME, new ButtonClickDelegate(quitButtonClick)));
        }

        public void startButtonClick(Button button)
        {
            m_Game.ChangeScreen(m_Game.m_GameMap);
        }

        public void aboutButtonClick(Button button)
        {
            m_Game.ChangeScreen(m_Game.m_GameTable);
        }

        public void quitButtonClick(Button button)
        {
            m_MenuSoundEffectInstance.Stop();
            m_Game.Exit();
        }

        public override void LoadContent()
        {
            m_MenuBackgroundTexture = m_Game.Content.Load<Texture2D>(Constants.MENU_BACKGROUND_ASSET_NAME);

            m_MenuSoundEffect = m_Game.Content.Load<SoundEffect>(Constants.GAME_MENU_SOUND_ASSET_NAME);
            m_MenuSoundEffectInstance = m_MenuSoundEffect.CreateInstance();
            m_MenuSoundEffectInstance.IsLooped = true;

            if (Constants.PLAY_SOUND)
            {
                m_MenuSoundEffectInstance.Play();
            }

            foreach (Button button in m_ButonList)
            {
                button.LoadContent();
            }
        }

        public override void UnloadContent()
        {
            foreach (Button button in m_ButonList)
            {
                button.UnloadContent();
            }

            m_MenuBackgroundTexture.Dispose();
            m_MenuSoundEffect.Dispose();
            m_MenuSoundEffectInstance.Dispose();
        }

        public override void Update(GameTime gameTime)
        {
            foreach (Button button in m_ButonList)
            {
                button.Update();
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(m_MenuBackgroundTexture, new Vector2(0, 0), Color.White);

            foreach (Button button in m_ButonList)
            {
                button.Draw(gameTime, spriteBatch);
            }
        }
    }
}
